package com.dbapp.common.response.page;

import com.dbapp.common.constant.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 表格分页数据对象
 *
 * @author ezra
 */
@Data
@ApiModel(value = "分页返回信息")
@NoArgsConstructor
public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 总记录数
     */
    @ApiModelProperty(value = "总记录数")
    private long total;

    /**
     * 列表数据
     */
    @ApiModelProperty(value = "每页的数据")
    private List<T> data;
    /**
     * 成功
     */
    public static final int SUCCESS = Constants.SUCCESS;

    /**
     * 失败
     */
    public static final int ERROR = Constants.FAIL;

    /**
     * 消息状态码
     */
    private int code;

    /**
     * 消息内容
     */
    private String msg;

    @ApiModelProperty("当前页")
    private Integer pageNo;
    @ApiModelProperty("每页记录数")
    private Integer pageSize;

    public static <T> PageResult<T> success(List<T> data,long total,PageDomain pageDomain) {
        return restResult(data,total, SUCCESS, null,pageDomain);
    }

    public static <T> PageResult<T> error()
    {
        return restResult(null,0, ERROR, null,null);
    }
    public static <T> PageResult<T> error(String msg)
    {
        return restResult(null,0, ERROR, msg,null);
    }
    private static <T> PageResult<T> restResult(List<T> data,long total, int code, String msg,PageDomain pageDomain) {
        PageResult<T> pageResult = new PageResult<>();
        if(null !=pageDomain){
            pageResult.setPageNo(pageDomain.getPageNum());
            pageResult.setPageSize(pageDomain.getPageSize());
        }
        pageResult.setData(data);
        pageResult.setMsg(msg);
        pageResult.setTotal(total);
        pageResult.setCode(code);
        return pageResult;
    }
}